<template>
  <div class="yl-container">

    <div class="yl-main">
      <!-- 搜索模块 -->
      <div class="yl-search-list">
        <el-form :model="searchConfig" ref="searchConfig" :inline="true">

          <el-form-item label="系别">
            <el-select v-model="searchConfig.department" placeholder="请选择系别" size="small">
              <el-option
                v-for="(item, index) in departments"
                :key="index"
                :value="item.label"
                :label="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="类型">
            <el-select v-model="searchConfig.kind" placeholder="请选择类型" size="small">
              <el-option
                v-for="(item, index) in kinds"
                :key="index"
                :value="item.value"
                :label="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="申报人">
            <el-select v-model="searchConfig.uploader" placeholder="请选择申报人" size="small">
              <el-option
                v-for="(item, index) in users"
                :key="index"
                :value="item.nns_account"
                :label="item.nns_name + '-' + item.nns_department"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <YlButton uiType="default" icon="el-icon-search" @click="initData(false)">搜 索</YlButton>
            <YlButton uiType="default" btnType="success" icon="iconfont yl-ecs-zhongzhi yl-icon" @click="initTable(true)">重 置</YlButton>
          </el-form-item>
        </el-form>
      </div>
      <br>

      <!-- table 列表组 -->
      <div class="yl-table-list">
        <el-table
          v-loading="loading"
          element-loading-text="玩命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          border
          :data="tableData"
          row-key="nns_code"
          height="550"
          default-expand-all
          :tree-props="{children: 'nns_children'}"
        >
          <el-table-column label="项目编号" prop="nns_code" width="200" show-overflow-tooltip></el-table-column>

          <el-table-column label="预算项目"  prop="nns_name" show-overflow-tooltip></el-table-column>

          <el-table-column align="center" label="数量" prop="nns_number" show-overflow-tooltip></el-table-column>

          <el-table-column align="center" label="类型" show-overflow-tooltip>
            <template slot-scope="{row}">
              <span>{{ row.nns_kind | formatKind(true) }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="系别" prop="nns_department" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="申报人" prop="nns_uploader_name" show-overflow-tooltip></el-table-column>

          <el-table-column align="center" label="预算金额(元)" show-overflow-tooltip>
            <template slot-scope="{row}">
              <span>{{ row.nns_money | formatMoney('yuan') }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="已申报金额(元)" show-overflow-tooltip>
            <template slot-scope="{row}">
              <span>{{ row.nns_used_money | formatMoney('yuan') }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="可申报金额(元)" show-overflow-tooltip>
            <template slot-scope="{row}">
              <span>{{ row.nns_can_use_money | formatMoney('yuan') }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center"  label="操作" width="200" fixed="right">
            <template slot-scope="{row}">
              <YlButton
                uiType="main"
                icon="iconfont yl-ecs-bianji yl-icon"
                v-show="row.hasOwnProperty('nns_can_write') === true && row.nns_can_write === true"
                @click="goto(row, 'write')"
              >
                <span>申报</span>
              </YlButton>

              <YlButton
                uiType="main"
                btnType="info"
                icon="iconfont yl-ecs-shanchu yl-icon"
                v-show="row.hasOwnProperty('nns_can_write') === true"
                @click="goto(row, 'log')"
              >
                <span>申报记录</span>
              </YlButton>
            </template>
          </el-table-column>

          <div class="yl-table-empty" slot="empty">
            <img src="~@/assets/icons/svg/no-data.svg">
            <div>暂无数据</div>
          </div>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import {
  initSelectValue
} from '@/middleware'
const Base64 = require('js-base64').Base64
export default {
  data () {
    return {
      // 类型数组
      kinds: [
        { value: '1', label: '行政' },
        { value: '2', label: '教学' },
        { value: '3', label: '全部' }
      ],
      // 系别列表
      departments: [],
      users: [],
      canWrite: false,
      loading: false,
      // 标题
      title: '',
      // 下载地址
      fileUrl: '',
      // table 列表数据
      tableData: [],
      searchConfig: {
        kind: '3',
        department: '',
        user: ''
      },
      // query信息
      query: {}
    }
  },
  created () {
    this.query = this.$route.query
    this.initTable()
  },
  methods: {
    // 列表初始化
    initTable (isForce = false) {
      this.dialogVisible = false
      this.initDepartment()
      this.initUploader()
      this.searchConfig = this.initForm(this.searchConfig)
      this.initData(isForce)
    },

    async initDepartment () {
      let data = await this.$ylapi.getDepartList()
      data = initSelectValue(data)
      this.departments = data
    },

    async initUploader () {
      let data = await this.$ylapi.getUserListSelect()
      this.users = data
    },

    // 数据初始化
    async initData (isForce = false) {
      this.loading = true
      let data = await this.$ylapi.getProjectListExecuteStageBillWrite({
        'stage_id': this.query.id,
        ...this.searchConfig
      })
      this.tableData = data
      this.loading = false
    },

    goto (row, to) {
      // 去报单填报界面
      let url = ''
      let data = {}
      if (to === 'write') {
        url = `/executeStage/billWrite/report/${row.nns_id}/${this.query.id}`
      } else if (to === 'log') { // 去填报记录页面
        url = '/executeStage/billWrite/showWriteLogList'
        data = {
          stage_id: this.query.id,
          mx_id: row.nns_id,
          canWrite: this.query.canWrite
        }
      }
      this.$router.push({
        path: `${url}`,
        query: data
      })
    },

    // 初始化
    getInitState (val) {
      if (val) {
        this.initTable(true)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
